package com.qubyer.okhttputil.builder;

import android.net.Uri;

import com.qubyer.okhttputil.request.GetRequest;
import com.qubyer.okhttputil.request.RequestCall;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by zhy on 15/12/14.
 */
public class GetBuilder extends OkHttpRequestBuilder<GetBuilder> {
    private final String salt = "qumaimai20201215";

    @Override
    public RequestCall build() {
        if(params == null){
            params = new TreeMap<>();
        }
        url = appendParams(url, params);
        return new GetRequest(url, tag, params, headers, id).build();
    }

    protected String appendParams(String url, Map<String, String> params) {
        if (url == null || params == null) {
            return url;
        }
        params.put("timestamp", String.valueOf(System.currentTimeMillis()).substring(0,10));
        Set<Map.Entry<String, String>> entries = params.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        StringBuilder stringBuilder = new StringBuilder();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            stringBuilder.append(entry.getKey());
            stringBuilder.append("=");
            stringBuilder.append(entry.getValue());
            stringBuilder.append("&");
        }
        String s = stringBuilder.toString();
        if (s.length() > 0) {
            s = s.substring(0, s.length() - 1);
        }
        s += salt;
        String sign = MD5(s).toUpperCase();
        params.put("sign",sign);

        Uri.Builder builder = Uri.parse(url).buildUpon();
        Set<Map.Entry<String, String>> keys = params.entrySet();
        Iterator<Map.Entry<String, String>> iterator2 = keys.iterator();
        while (iterator2.hasNext()) {
            Map.Entry<String, String> entry = iterator2.next();
            builder.appendQueryParameter(entry.getKey(), entry.getValue());
        }
        return builder.build().toString();
    }

    public static String MD5(String in) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(in.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }

    public GetBuilder params(Map<String, String> params) {
        if (this.params == null) {
            this.params = new TreeMap<>();
        }
        this.params.putAll(params);
        return this;
    }

    public GetBuilder addParams(String key, String val) {
        if (this.params == null) {
            params = new TreeMap<>();
        }
        params.put(key, val);
        return this;
    }
}
